﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using core;

namespace comix
{
    public partial class bookdetails : System.Web.UI.Page
    {
        private Book book;

        private static int CompareFileInfos(FileInfo f1, FileInfo f2)
        {
            int n1 = Convert.ToInt32(f1.Name.Substring(0,f1.Name.Length-4));
            int n2 = Convert.ToInt32(f2.Name.Substring(0,f2.Name.Length-4));
            return n1.CompareTo(n2);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LCustomer customer = (LCustomer)Session["customer"];
            book = new Book();
            book.Id = Convert.ToInt32(Request.QueryString["id"]);
            book.FillItem();
            lbl_title.Text = book.Title;
            thumb.ImageUrl = book.LargeThumbnailPath();
            if (book.Owned)
            {
                buy.Text = "ΑΓΟΡΑΣΜΕΝΟ";
                buy.NavigateUrl = "";
            }
            else
            {
                buy.Text = "ΣΤΟ ΚΑΛΑΘΙ - " + book.Price.ToString() + "€";
                if (customer.Id == -1)
                    buy.Attributes.Add("onclick", "alert('Για να προχωρήσετε σε αγορές θα πρέπει να έχετε κάνει LogIn στο σύστημα');parent.showAccount();parent.$.fancybox.close();return false;");
                else
                    buy.Attributes.Add("onclick", "parent.addToCartAjax(" + book.Id.ToString() + ");parent.$.fancybox.close();return false;");
            }
            ratebuttr.Visible = book.Owned;            
            if (customer.Id == -1)
                sample.Attributes.Add("onclick", "alert('Για να κατεβάσετε κάποιο δείγμα θα πρέπει να έχετε κάνει LogIn στο σύστημα');parent.showAccount();parent.$.fancybox.close();return false;");
            else
            {
                sample.Attributes.Add("onclick", "getSampleAjax(" + book.Id.ToString() + "," + customer.Id.ToString() + ");return false;");
            }
            sample.Enabled = !book.Owned && !book.DemoOwned;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<b>Κείμενα:</b><br>");
            AuthorsCollection authors = book.AuthorsOfBook;
            foreach (Author a in authors.Items)
            {
                sb.Append(a.FullName + "<br>");
            }
            sb.Append("<br><b>Εικονογράφηση:</b><br>");
            AuthorsCollection designers = book.DesignersOfBook;
            foreach (Author a in designers.Items)
            {
                sb.Append(a.FullName + "<br>");
            }
            sb.Append("<br><b>Εκδόσεις:</b><br>");
            sb.Append(book.Series.Publisher.Name);
            sb.Append("<br><br><b>Σελίδες: </b>");
            sb.Append(book.Npages.ToString());
            sb.Append("<br><br>");
            sb.Append(book.Description);
            TextBox1.Text = sb.ToString();

            System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
            string path = MapPath("previews/wb"+book.Id.ToString());
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists)
            {
                FileInfo[] fis = di.GetFiles("*.jpg");
                List<FileInfo> fff = new List<FileInfo>();
                foreach (FileInfo fi in fis)
                    fff.Add(fi);
                fff.Sort(CompareFileInfos);
                foreach (FileInfo fi in fff)
                {
                    sb2.Append("<li><img src=\"previews/wb" + book.Id.ToString() + "/" + fi.Name + "\" alt=\"\" /></li>");
                }

                Literal1.Text = sb2.ToString();
            }
        }
    }
}
